package utils

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
)

func HttpGet(url string) []byte {
	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	readAll, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln("读取http的body数据失败：", err)
	}
	return readAll
}

func httpRegex(str string) []string {

	//正则表达式，有点菜，只会(.*?)
	regex := "<span class=" + "\"" + "img-hash" + "\"" + ">(\\s)(.*?)(\\s)</span>"

	reg := regexp.MustCompile(regex)

	dataS := reg.FindAllSubmatch([]byte(str), -1)

	results := make([]string, 0)

	for _, v := range dataS {

		results = append(results, string(v[1]))
	}

	return results

}

func HttpRegex(bytes []byte) []string {
	// 正则表达式

	//文件夹 <td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	//            <img src='/thredds/folder.gif' alt='Folder'> &nbsp;<a href='/thredds/catalog/testEnhanced/catalog.html'><tt>ETA Data/</tt></a></td>
	// 文件 <td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	//            <a href='catalog.html?dataset=testDatasetScan/testgrid1.nc'><tt>testgrid1.nc</tt></a></td>
	//regex := "<td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(.*?)</td>"
	regex := "<td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\\s(.*?)"
	mustCompile := regexp.MustCompile(regex)
	allSubmatch := mustCompile.FindAllSubmatch(bytes, -1)
	for key, value := range allSubmatch {
		fmt.Println(key, "       ", string(value[key]))
	}

	return nil
}
